import { CHANGE_INPUT_VALUE, ADD_ITEM_LIST,  DELATE_ITEM_LIST, GET_INIT_LIST } from "./actionTypes";


const initState = {
    inputValue: "",
    list:[]
}
export default (state=initState, action)=>{
    
    if (action.type === CHANGE_INPUT_VALUE){
        const newState = JSON.parse(JSON.stringify(state))
        newState.inputValue = action.inputValue
        return newState
    }

    if (action.type === ADD_ITEM_LIST){
        const newState = JSON.parse(JSON.stringify(state))
        newState.list.push(newState.inputValue)
        newState.inputValue = ""
        return newState
    }

    if (action.type ===  DELATE_ITEM_LIST){
        const newState = JSON.parse(JSON.stringify(state))
        newState.list.splice(action.index, 1)
        return newState
    }

    if (action.type ===  GET_INIT_LIST){
        const newState = JSON.parse(JSON.stringify(state))
        newState.list = action.list
        return newState
    }

    return state
}